package freespiritsocial.io.user.ui.adapter

import androidx.collection.ArrayMap
import androidx.collection.arrayMapOf
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import freespiritsocial.io.user.R
import freespiritsocial.io.user.data.model.bean.UserInfo
import freespiritsocial.io.user.databinding.ItemCheckPeopleBinding

class CheckPeopleAdapter
    (
    data: ArrayList<UserInfo> = arrayListOf(),
    layoutResIdRes: Int = R.layout.item_check_people
):
    BaseQuickAdapter<UserInfo,BaseDataBindingHolder<ItemCheckPeopleBinding>>(layoutResIdRes,data) {
    val selectLis: ArrayMap<String,Int> = arrayMapOf()

    init {
        setOnItemClickListener { adapter,view,position ->
            val resourceId = data[position].resourceId
            val value = selectLis.getOrDefault(resourceId,-1)
            selectLis[resourceId] = if (value != -1) -1 else position
            notifyItemChanged(position)
        }
    }


    override fun convert(holder: BaseDataBindingHolder<ItemCheckPeopleBinding>,item: UserInfo) {
        holder.dataBinding?.let {
            it.ivCheck.setImageResource(
                if (selectLis.getOrDefault(
                        item.resourceId,
                        -1
                    ) == holder.bindingAdapterPosition
                ) R.mipmap.icon_people_select else R.mipmap.icon_people_unselect
            )
            it.user = item
        }
    }

    fun dealData(userInfo: UserInfo) {
        when (userInfo.eventStatus) {
            UserInfo.STATUS_ADD -> addData(userInfo)
            UserInfo.STATUS_DEL -> remove(userInfo)
        }
    }
}